<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
</style>
<link rel="stylesheet" href="css/weui.css"/>
    <link rel="stylesheet" href="css/example.css"/>
<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=cR98TwX2FnKTpANIOYhMYMot9a4ioxjc"></script>
<title>导航路线</title>
</head>
<body>
<div id="allmap"></div>

<div class="weui-tabbar">
    <a href="javascript:;" class="weui-tabbar__item" id="drive">
      <div class="weui-tabbar__icon">
        <img src="../../img/drive.jpg" alt="">
      </div>
      <p class="weui-tabbar__label">驾车</p>
    </a>
    <a href="javascript:;" class="weui-tabbar__item" id="walk">
      <div class="weui-tabbar__icon">
        <img src="../../img/walk.jpg" alt="">
      </div>
      <p class="weui-tabbar__label">步行</p>
    </a>
  </div>

</body>
</html>
<script type="text/javascript">
	function getQueryString(name) {
    	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    	var r = window.location.search.substr(1).match(reg);
    	if (r != null) return unescape(r[2]); return null;
    }
	
	var longitude=getQueryString("longitude");
	var latitude=getQueryString("latitude");
	var longitude2=getQueryString("longitude2");
	var latitude2=getQueryString("latitude2");

	
	// 百度地图API功能
	var map = new BMap.Map("allmap");
	map.centerAndZoom(new BMap.Point(longitude, latitude), 11);

	var p1 = new BMap.Point(longitude,latitude);
	var p2 = new BMap.Point(longitude2,latitude2);

	var distance=getDistance();
	
	if(distance>1){
		//驾车
		var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true}});
	
		driving.search(p1, p2);
		
		$("title").html("驾车导航路线");
	}else{
		//步行
		var walking = new BMap.WalkingRoute(map, { 
	    	renderOptions: { 
	        	map: map, 
	        	autoViewport: true 
	    	}
		});
	
		walking.search(p1,p2);
		
		$("title").html("步行导航路线");
	}
	
	$("#drive").on("click",function(){
		map.clearOverlays();
		
		var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true}});
		
		driving.search(p1, p2);
		
		document.title = "驾车导航路线";
	});
	
	$("#walk").on("click",function(){
		map.clearOverlays();
		
		//步行
		var walking = new BMap.WalkingRoute(map, { 
	    	renderOptions: { 
	        	map: map, 
	        	autoViewport: true 
	    	}
		});
	
		walking.search(p1,p2);
		
		document.title = "步行导航路线";
	});
	
	 function getDistance(){
	    	var pointA = new BMap.Point(longitude,latitude);  // 创建点坐标A--大渡口区
	    	var pointB = new BMap.Point(longitude2,latitude2);  // 创建点坐标B--江北区
	    	var d=map.getDistance(pointA,pointB)/1000;
	    	return d;  //获取两点距离,保留小数点后两位
	    }
	
</script>